//
//  AddressTableViewCell.swift
//  MikerShop
//
//  Created by eme on 2016/11/26.
//  Copyright © 2016年 eme. All rights reserved.
//
/*
 owner:gyh
 update:2016年11月23日16:14:12
 info: 编辑地址Cell
 */
import UIKit
import RxSwift
import RxCocoa
import RickonCore

class AddressTableViewCell: UITableViewCell {
    
    @IBOutlet weak var nameLabel: UILabel!      //姓名Label
    @IBOutlet weak var phoneLabel: UILabel!     //电话Label
    @IBOutlet weak var addressLabel: UILabel!   //地址Label
    @IBOutlet weak var defaultButton: UIButton! //默认按钮
    @IBOutlet weak var editorButton: UIButton!  //编辑按钮
    @IBOutlet weak var deleteButton: UIButton!  //删除按钮
    
    @IBOutlet weak var selectedImageView: UIImageView!  //是否选中图片
    
    @IBOutlet weak var seperateLine_V: UIView!
    var disposeBag = DisposeBag()
    //删除操作
    var editAddress_Block:((_ addressModel:AddressModel) -> Void)?
    //删除操作
    var deleteAddress_Block:((_ addressModel:AddressModel) -> Void)?
    //设置默认操作
    var setDefaultAddress_Block:((_ addressModel:AddressModel) -> Void)?
    
    var item :AddressModel?{
        didSet{
            if let item = self.item {
                self.phoneLabel.text = item.cellphone
                self.nameLabel.text = item.recipients
                self.addressLabel.text = "收货地址: " +  "\(item.province)\(item.city)\(item.area)\(item.adress)"
                if item.flag == "0" {
                    self.selectedImageView.image = UIImage(named: "The selected", in: AryaCore.bundle, compatibleWith: nil)
                }else{
                    self.selectedImageView.image = UIImage(named: "not clicked", in: AryaCore.bundle, compatibleWith: nil)
                }
            }
        }
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        self.seperateLine_V.backgroundColor = Theme.v.separate_Line
        deleteButton.adjustsImageWhenDisabled = false
        self.defaultButton
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                if let item = self.item{
                    item.flag = item.flag == "0" ? "1" : "0"
                    self.setDefaultAddress_Block?(item)
                }
            })
            .disposed(by: disposeBag)
        self.editorButton
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                self.editAddress_Block?(self.item!)
            })
            .disposed(by: disposeBag)
        self.deleteButton
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                self.deleteAddress_Block?(self.item!)
            })
            .disposed(by: disposeBag)
        
    }
    
    
}
